package net.dipper.tankwar.editor.ui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.core.helper.ViewPort;

import wnb.util.ui.AnimationPane;

public class MiniMap extends AnimationPane implements MouseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private EditorContext context;

	public MiniMap(EditorContext context, int delay, boolean loop) {
		super(delay, loop);
		
		this.context = context;
		MiniMapAnimation animation = new MiniMapAnimation(context);
		setAnimation(animation);
		
		addMouseListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Point p = e.getPoint();
		Dimension size = getSize();
		ObjectMap map = context.getMap();
		ViewPort v = context.getViewPort();
		int gWidth = size.width / map.getWidth();
		int gHeight = size.height / map.getHeight();
		int x = p.x / gWidth - v.width / 2;
		int y = p.y / gHeight - v.height / 2;
		if(x < 0){
			x = 0;
		}else if(x + v.width >= map.getWidth()){
			x = map.getWidth() - v.width;
		}
		if(y < 0){
			y = 0;
		}else if(y + v.height >= map.getHeight()){
			y = map.getHeight() - v.height;
		}
		
		context.setViewPort(x, y, v.width, v.height);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
